<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="formModel">
        <el-form-item class="no-warn-item" label="用户登录名:" prop="username">
          <el-input
            v-model="formModel.username"
            clearable
          />
        </el-form-item>
        <el-form-item class="no-warn-item" label="员工号:" prop="engineerCode">
          <el-input
            v-model="formModel.engineerCode"
            clearable
          />
        </el-form-item>
        <el-form-item class="no-warn-item" label="真实姓名:" prop="realName">
          <el-input
            v-model="formModel.realName"
            clearable
          />
        </el-form-item>
        <el-form-item class="no-warn-item" label="所属部门:" prop="depId">
          <el-input
            v-model="formModel.depId"
            disabled
            clearable
          />
        </el-form-item>
        <el-row class="submit-area">
          <el-col :span="24">
            <el-button type="primary" v-if="hasPermission('user_select')" plain @click="queryList(false)">查询</el-button>
            <el-button type="primary" v-if="hasPermission('user_add')" plain @click="handleAdd">新增用户</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <h3>用户信息列表:</h3>
    <hey-table ref="table" :columns="columns" @row-click="handleRowClick" :is-radio="true">
      <template slot="tools">
        <el-table-column
          label="操作"
          width="120"
          fixed="right"
          header-align="center"
          align="center"
        >
          <template slot-scope="{row}">
            <span class="hey_tools_btn" v-if="hasPermission('user_edit')" @click="handleEdit(row)">编辑</span>
            <span class="hey_tools_btn" v-if="hasPermission('user_del')" @click="handleDelete(row)">删除</span>
          </template>
        </el-table-column>
      </template>
    </hey-table>
    <h3>用户角色列表:</h3>
    <hey-table ref="table1" :table-data="tableData1" :columns="columns1" :auto-width="false"/>
    <el-dialog
      v-dialogDrag
      v-if="dialogVisible"
      :close-on-click-modal="false"
      title="用户信息"
      :visible.sync="dialogVisible"
      width="80%"
    >
      <user-mnt :page-type="operType" :edit-data="editData" @mntSuc="queryList" />
    </el-dialog>

  </div>
</template>

<script>
  export default {
    name: 'UserList',
    components: {
      UserMnt: () => import('./user-mnt')
    },
    props: {},
    data() {
      return {
        dialogVisible: false,
        // userTypeOptions: [],
        operType: '',
        editData: {},
        tableData1:[],
        formModel: {
          username: '',
          isDelete: '0',
          engineerCode: '',
          realName: '',
          depId: '',
        },
        columns: [
          { title: '用户登录名', key: 'username'},
          { title: '员工号', key: 'engineerCode'},
          { title: 'SAP用户名', key: 'sapUsername'},
          { title: '真实姓名', key: 'realName'},
          { title: '用户描述', key: 'description'},
          { title: '所属部门', key: 'depName'},
          { title: '办公地址', key: 'officeLocation'},
          { title: '电话号码', key: 'telephone'},
          { title: '电子邮件', key: 'email'},
          { title: '传真', key: 'userFax'},
          { title: '登录状态', key: 'loginStatus'},
          { title: '最后登录时间', key: 'lastLoginTime'},
          { title: '角色名称', key: 'roleName'},
          { title: '创建时间', key: 'createTime' },
          { title: '创建人', key: 'createUsername' },
          { title: '最后修改时间', key: 'lastUpdateTime'},
          { title: '最后修改人', key: 'lastUpdateUsername' },
        ],
        columns1: [
          { title: '角色名称', key: 'roleName' },
          { title: '创建时间', key: 'createTime' },
          { title: '创建人', key: 'createUsername' },
          { title: '最后修改时间', key: 'lastUpdateTime'},
          { title: '最后修改人', key: 'lastUpdateUsername' },
        ],
      }
    },
    computed: {

    },
    mounted() {

    },
    methods: {
      handleRowClick(row) {
        if (row) {
          const param = {}
          param.isDelete = '0'
          param.userid = row.userid
          this.$refs.table1.query('/user/queryTmSysUserRoles', param, '', false)
        } else {
          //清空子表数据
          this.tableData1 = []
        }
      },
      queryList() {
        this.tableData1 = []
        this.$refs.table.query('/user/queryTmSysUser', this.formModel, '', false)
      },
      handleAdd() {
        this.operType = 'add'
        this.dialogVisible = true
      },
      handleEdit(row) {
        const param = {pageNum:1,pageSize:9999}
        param.userid = row.userid
        param.isDelete = '0'
        this.get('/user/queryTmSysUserRoles', param).then(res => {
          if(res.list){
            const arr = res.list
            row.roleIdList = []
            for(let i=0;i<arr.length;i++){
              row.roleIdList.push(arr[i].roleId)
            }
            this.operType = 'edit'
            this.editData = row || {}
            this.dialogVisible = true
          }
        })
      },
      handleDelete(row) {
        this.$sure('确认删除？', _ => {
          this.post('/user/maintainUser', { userid: row.userid,operateType:'03'}).then(_ => {
            this.$info('删除成功', this.queryList)
          })
        })
      },
    }
  }
</script>

<style lang="scss" scoped>

</style>
